#include <assert.h>
#include <string.h>

#include "api.c"

void assert_set_err(const char* value)
{
    set_err(value);

    assert(strcmp(value, store) == 0);
    assert(strlen(value) == strlen(store));
}

void assert_set_and_get_err(const char* value, char* buffer, size_t size)
{
    assert_set_err(value);

    int written = get_err(buffer, size);

    assert(strcmp(buffer, value) == 0);
    assert(strlen(value) == strlen(buffer));
    assert(strlen(value) == written);
}

void assert_setting_values_from_small_to_large_and_vice_versa() {
    assert_set_err("");
    assert_set_err("a");
    assert_set_err("ab");
    assert_set_err("abc");
    assert_set_err("abcdefghijklmnopqrstuvwxyz");
    assert_set_err("abc");
    assert_set_err("ab");
    assert_set_err("a");
    assert_set_err("");
}

void test_api() {
    {
        assert_setting_values_from_small_to_large_and_vice_versa();
        assert_setting_values_from_small_to_large_and_vice_versa();
    }
    {
        char buffer[1];
        assert_set_and_get_err("", buffer, 0);
    }
    {
        char buffer[2];
        assert_set_and_get_err("a", buffer, 1);
    }
    {
        char buffer[3];
        assert_set_and_get_err("ab", buffer, 2);
    }
    {
        char buffer[4];
        assert_set_and_get_err("abc", buffer, 3);
    }
    {
        char buffer[27];
        assert_set_and_get_err("abcdefghijklmnopqrstuvwxyz", buffer, 26);
    }
}

int main(void)
{
    test_api();

    return 0;
}
